<?php
//======================================================================================
//
// Function: Save HUB_IMPORT
//
// Programmer: JKJ
// Date : 2024-04-24
//
// Copyright Reeft A/S (c) - 2024
//======================================================================================
//======================================================================================
// General config
//======================================================================================
include "config/config.php";
//======================================================================================
// Get input
//======================================================================================
if (isset($_REQUEST["mode"])) $mode = $_REQUEST["mode"];
else $mode = '';
if (isset($_REQUEST["data"])) {
$data = $_REQUEST["data"];
} else {
$data = 'hovsa';
}
if ( $mode == '' ) {
echo "There's no action....1";
exit;
}
if ( $mode <> '*add' and $mode <> '*update' ) {
echo "There's no action....2";
exit;
}
if ( $data == 'hovsa' ) {
echo "There's no data....";
exit;
}
//======================================================================================
// Convert from JSON
//======================================================================================
// $json = json_decode($data);
// print_r($data);
// echo "\n\n";
//exit;
// Original JSON data
$jsonData = $data;
// Decode the JSON string into an associative array
$data = json_decode($jsonData, true);
// Initialize the new structure
$newJson = [
"header" => [
"APIKEY" => "some-api-key from HUB_CONFIG/WEBSERVICE_TOKEN",
"mode" => "*UPDATE"
],
"detail" => []
];
// Iterate through the original data to build the 'detail' section
foreach ($data as $item) {
// Extract 'name' and 'value' from each item
$name = str_replace("modal-input-", "", $item['name']);
$value = $item['value'];
// Handle special cases
if ($name === "IPIMPSEQ") {
// Convert 'IPIMPSEQ' value to integer
$value = (int)$value;
} elseif (strpos($name, "IPCPY") !== false) {
// Change '*ADD' to '*REPLACE' for 'IPCPYOPT' field
if ($name === "IPCPYOPT" && $value === "*ADD") {
// $value = "*REPLACE";
}
}
// Add to the 'detail' section
$newJson['detail'][$name] = $value;
}
// Convert the new structure back to JSON
$newJsonString = json_encode($newJson, JSON_PRETTY_PRINT);
// Output the new JSON
//echo $newJsonString;
$url = 'http://10.30.1.43:81/cgi-bin/hub_import';
// Create a new cURL resource
$ch = curl_init($url);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $newJsonString);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt($ch, CURLOPT_HEADER, false); // we do not need headers
// curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
// Execute the POST request
$result = curl_exec($ch);
$ary = curl_getinfo($ch);
// Close cURL resource
curl_close($ch);
print_r($result);
exit;
$break = "\r\n";
$stringData = '<?xml version="1.0" encoding="UTF-8"?>' . $break;
$stringData .= '<data>' . $break;
foreach($json as $obj){
$key = $obj->name;
$value = $obj->value;
if ( $key == 'modal-input-pk_department') {
$pk_department = $value;
}
else if ( $key == 'modal-input-department_code') {
$department_code = $value;
}
else if ( $key == 'modal-input-department_name') {
$department_name = $value;
}
else if ( $key == 'modal-input-department_image') {
$department_image = $value;
}
else if ( $key == 'modal-input-department_color') {
$department_color = $value;
}
else if ( $key == 'modal-input-department_active') {
$department_active = $value;
}
}
//======================================================================================
// Get session variables
//======================================================================================
include "include/getsession.php";
//======================================================================================
// Set language
//======================================================================================
include "include/set_language.php";
//======================================================================================
// Connect to SQLite database in file
//======================================================================================
header('Content-Type: text/xml; charset=UTF-8');
$currentDate = date('Y-m-d');
$currentTime = date('H:i:s');
$xmlString = "<?xml version=\"1.0\"?>\n";
$xmlString .= "<data>\n";
//======================================================================================
// Connect to some DB
//======================================================================================
include "include/db_connect.php";
//======================================================================================
// Set active, token and other default values
//======================================================================================
if ( $department_code == '' ) {
$department_code = date("Y-m-d H:i:s");
$department_code = gmdate('Y-m-d H:i:s', strtotime($department_code));
}
//======================================================================================
// Create SQL
//======================================================================================
if ( $mode == '*update')
{
$sql = "update visual_department
set
department_code = '$department_code',
department_name = '$department_name',
department_image = '$department_image',
department_color = '$department_color',
department_active = '$department_active'
where pk_department = $pk_department
";
}
if ( $mode == '*add')
{
$sql = "INSERT INTO visual_department
(
department_code,
department_name,
department_image,
department_color,
department_active
)
VALUES (
'$department_code',
'$department_name',
'$department_image',
'$department_color',
'$department_active'
)
";
}
//echo $sql;
//======================================================================================
// Get data
//======================================================================================
include "include/db_run_sql.php";
//======================================================================================
// Check sql
//======================================================================================
if ( $current_elm == 0 ) {
$returnCode = '99';
$returnMsg = 'No data found';
} else {
$returnCode = '00';
$returnMsg = 'Alles ist gut';
}
//======================================================================================
// Create header
//======================================================================================
$xmlString .= "\t<header>\n";
$xmlString .= "\t\t<mode>" . $mode . "</mode>\n";
$xmlString .= "\t\t<last_pk_id>" . $last_id . "</last_pk_id>\n";
$xmlString .= "\t\t<rows_updated>" . $current_elm . "</rows_updated>\n";
$xmlString .= "\t\t<currentDate>" . $currentDate . "</currentDate>\n";
$xmlString .= "\t\t<currentTime>" . $currentTime . "</currentTime>\n";
$xmlString .= "\t\t<returnCode>" . $returnCode . "</returnCode>\n";
$xmlString .= "\t\t<returnMsg>" . $returnMsg . "</returnMsg>\n";
$xmlString .= "\t</header>\n";
//======================================================================================
// Paint it black
//======================================================================================
$xmlString = $xmlString . "</data>\n";
echo $xmlString;
?>